package com.zou.demo.bootstrapcurd.controller;

import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @author: 邹祥发
 * @date: 2021/6/18 16:13
 */
@Controller
public class fileController {

    @RequestMapping(value = "/file", method = RequestMethod.GET)
    public String file() {
        return "file";
    }

    //单文件上传
    @ResponseBody
    @PostMapping("upload")
    public String upload(MultipartFile file, HttpServletRequest request) {
        if (file.isEmpty()) {
            //文件为空时
            return "文件为空";
        } else {
            // 获取当前时间
            String current_date = new SimpleDateFormat("/yyyy/MM/dd/").format(new Date());
            // 保存文件的目录
            String folder = request.getServletContext().getRealPath("/img") + current_date;
            // 判断是否需要创建目录
            File folderFile = new File(folder);
            if (!folderFile.exists())
                folderFile.mkdirs();

            String filename = file.getOriginalFilename();   // 文件名
            filename = UUID.randomUUID().toString().replace("-", "") + filename.substring(filename.lastIndexOf("."));

            try {
                File destFile = new File(folderFile, filename);
                System.out.println(destFile.getAbsolutePath());
                file.transferTo(destFile);
                String url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/img" + current_date + filename;
                System.out.printf("上传成功，图片路径：%s%n", url);
                return "success";
            } catch (IOException e) {
                return "上传失败！";
            }
        }
    }

    @RequestMapping(value = "/download.do", method = RequestMethod.GET)
    public ResponseEntity<byte[]> download() throws IOException {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        //给要下载的文件起一个名字
        String downName = new String("01.mp4".getBytes(StandardCharsets.UTF_8), "iso8859-1");
        headers.setContentDispositionFormData("attachment", downName);
        System.out.println("下载的文件名：" + downName);
        //d盘中的redis.mp4是待下载的视频。这里讲待下载的视频放在d盘，也可以放在项目目录下
        return new ResponseEntity<>(FileUtils.readFileToByteArray(new File("/opt/java/springboot/file/01.mp4")),
                headers, HttpStatus.CREATED);
    }

}
